package com.sskj.mine.ui.activity;


import android.support.design.widget.BottomSheetDialog;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.AppCircleCheckUtil;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.lib.util.TipUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.presenter.SecurityCenterActivityPresenter;

import java.util.HashMap;

import javax.inject.Inject;

import butterknife.BindView;


/**
 * 安全中心
 */
@Route(path = RConfig.MINE_SECURITY_CENTER)
public class SecurityCenterActivity extends BaseActivity<SecurityCenterActivityPresenter> {
    @BindView(R2.id.switchSms)
    CheckBox switchSms;
    @BindView(R2.id.llSmsOpen)
    LinearLayout llSmsOpen;
    @BindView(R2.id.tvGoogleStatus)
    TextView tvGoogleStatus;
    @BindView(R2.id.llGoogleBind)
    LinearLayout llGoogleBind;
    @BindView(R2.id.switchGoogle)
    CheckBox switchGoogle;
    @BindView(R2.id.llGoogleOpen)
    LinearLayout llGoogleOpen;
    @BindView(R2.id.tvIdentityStatus)
    TextView tvIdentityStatus;
    @BindView(R2.id.llIdentity)
    LinearLayout llIdentity;
    @BindView(R2.id.tvLoginPwdStatus)
    TextView tvLoginPwdStatus;
    @BindView(R2.id.llLoginPwd)
    LinearLayout llLoginPwd;
    @BindView(R2.id.tvTradePwdStatus)
    TextView tvTradePwdStatus;
    @BindView(R2.id.llTradePwd)
    LinearLayout llTradePwd;
    private BottomSheetDialog bottomSheetDialogGoogle;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private BottomSheetDialog bottomSheetDialogSms;

    @Override
    protected int getLayoutId() {
        return R.layout.mine_activity_security_center;
    }

    @Override
    public SecurityCenterActivityPresenter getPresenter() {
        return new SecurityCenterActivityPresenter();
    }

    @Override
    protected void initView() {
        DaggerUserDataComponent.create().inject(this);
        setTitle(App.INSTANCE.getString(R.string.mine_securityCenterActivity1));
        ClickUtil.click(llLoginPwd, () -> {
            if (!AppCircleCheckUtil.checkGoogleSms(this, userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.MINE_SET_PAY_PWD).withInt(Constans.TYPE, 0).navigation();
        });
        ClickUtil.click(llTradePwd, () -> {
            if (!AppCircleCheckUtil.checkGoogleSms(this, userData)) {
                return;
            }
            if (userData.getConfig().getPayment_password_set()==0) {
                ARouter.getInstance().build(RConfig.MINE_SET_PAY_PWD).withInt(Constans.TYPE, 1).navigation();
            }else {
                ARouter.getInstance().build(RConfig.MINE_SET_PAY_PWD).withInt(Constans.TYPE, 2).navigation();

            }
        });
        ClickUtil.click(llGoogleBind, () -> {
            if (userData.getConfig().getGoogle_bind() == 0) {
                ARouter.getInstance().build(RConfig.MINE_BIND_GOOGLE).navigation();
            } else {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_securityCenterActivity2));
            }

        });

        ClickUtil.click(llGoogleOpen, () -> {
            if (switchGoogle.isChecked() && !switchSms.isChecked()) {
                TipUtil.getSureCancelTip(SecurityCenterActivity.this, App.INSTANCE.getString(R.string.mine_securityCenterActivity3), App.INSTANCE.getString(R.string.mine_securityCenterActivity4), () -> {

                });
                return;
            }
            bottomSheetDialogGoogle = BottomSheetUtil.showCheck(this, false, true, false, str -> {
                bottomSheetDialogGoogle.dismiss();
                mPresenter.toggleGoogle(!switchGoogle.isChecked(), str[1]);

            });
            bottomSheetDialogGoogle.show();
        });
        ClickUtil.click(llSmsOpen, () -> {
            if (!switchGoogle.isChecked() && switchSms.isChecked()) {
                TipUtil.getSureCancelTip(SecurityCenterActivity.this, App.INSTANCE.getString(R.string.mine_securityCenterActivity3), App.INSTANCE.getString(R.string.mine_securityCenterActivity4), () -> {

                });
                return;
            }
            bottomSheetDialogSms = BottomSheetUtil.showCheck(this, false, false, true, str -> {
                bottomSheetDialogSms.dismiss();
                mPresenter.toggleSms(!switchSms.isChecked(), str[2]);

            });
            bottomSheetDialogSms.show();

        });

    }

    @Override
    protected void initData() {
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                updateUI();
            } else {
                userData = null;
            }
        });
    }

    private void updateUI() {
        tvTradePwdStatus.setText(userData.getConfig().getPayment_password_set() == 0 ? App.INSTANCE.getString(R.string.mine_securityCenterActivity5) : App.INSTANCE.getString(R.string.mine_securityCenterActivity6));
        tvLoginPwdStatus.setText(App.INSTANCE.getString(R.string.mine_securityCenterActivity6));
        HashMap<Integer, String> identityMap = new HashMap<>();
        identityMap.put(0, App.INSTANCE.getString(R.string.mine_securityCenterActivity7));
        identityMap.put(1, App.INSTANCE.getString(R.string.mine_buyRecordFragment1));
        identityMap.put(2, App.INSTANCE.getString(R.string.mine_securityCenterActivity9));
        identityMap.put(3, App.INSTANCE.getString(R.string.mine_shellRecordFragment3));
        tvIdentityStatus.setText(identityMap.get(userData.getAuthentication()));
        switchGoogle.setChecked(!(userData.getConfig().getGoogle_verify() == 0));
        switchSms.setChecked(!(userData.getConfig().getSms_verify() == 0));
        llGoogleOpen.setVisibility(userData.getConfig().getGoogle_bind() == 0 ? View.GONE : View.VISIBLE);
        tvGoogleStatus.setText(userData.getConfig().getGoogle_bind() == 0 ? App.INSTANCE.getString(R.string.mine_securityCenterActivity11) : App.INSTANCE.getString(R.string.mine_securityCenterActivity12));
        ClickUtil.click(llIdentity, () -> {
            switch (userData.getAuthentication()){
                case 0:
                    ARouter.getInstance().build(RConfig.MINE_IDENTITY_VERIFY).navigation();
                    break;
                case 1:
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_securityCenterActivity13));
                    break;
                case 2:
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_securityCenterActivity14));
                    break;
                case 3:
                    ARouter.getInstance().build(RConfig.MINE_VERIFY_RESULT).withBoolean(Constans.IS_SUCCESS,false).navigation();
                    break;
            }

        });
    }

    /**
     * 开启关闭短信验证成功
     */
    public void toggleSmsSuccess() {
        userViewModel.update();
        if (switchSms.isChecked()) {
            switchSms.setChecked(false);
        } else {
            switchSms.setChecked(true);

        }
    }

    /**
     * 开启关闭短信验证成功
     */
    public void toggleGoogleSuccess() {
        userViewModel.update();
        if (switchGoogle.isChecked()) {
            switchGoogle.setChecked(false);
        } else {
            switchGoogle.setChecked(true);

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (userViewModel!=null){
            userViewModel.update();
        }
    }
}
